package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.Keyword;
import com.abaaba.project2.bean.KeywordExample;
import com.abaaba.project2.bean.SearchHistory;
import com.abaaba.project2.bean.SearchHistoryExample;
import com.abaaba.project2.bean.vo.wx.WxSearchIndexVO;
import com.abaaba.project2.mapper.KeywordMapper;
import com.abaaba.project2.mapper.SearchHistoryMapper;

import com.abaaba.project2.service.wx.WxSearchHistoryService;
import com.abaaba.project2.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Hedgehog
 * @Date 2021/6/8 10:17
 * @Version 1.0
 */
@Service
public class WxSearchHistoryServiceImpl implements WxSearchHistoryService {
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public WxSearchIndexVO search() {
        //需要：1.keywordlist  2.history中keyword的list  3.keyword中ishot的那个
        //0.创建相对应的一个大vo和两个vo中的list，在for循环中创建三个子vo
        //1.查询keyword表拿出一个list，
        //2.1遍历list1 封装到vo对象中，之后封装到volist中
        //2.2如果这个对象中ishot是true，把这个对象封装到子vo中
        //3.查询history表拿出一个list2.
        //4.for循环遍历list2，拿到key值，封装到小vo中，封装到对应volist中
        //5.全部封装到大vo中

        //0
        WxSearchIndexVO wxSearchIndexVO = new WxSearchIndexVO();
        //两个list
        ArrayList<WxSearchIndexVO.HotKeywordListBean> hotKeywordListBeans = new ArrayList<>();
        ArrayList<WxSearchIndexVO.HistoryKeywordListBean> historyKeywordListBeans = new ArrayList<>();
        //一个vo
        WxSearchIndexVO.DefaultKeywordBean defaultKeywordBean = new WxSearchIndexVO.DefaultKeywordBean();

        //1.
        KeywordExample keywordExample = new KeywordExample();
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        //2.1
        for (Keyword keyword : keywords) {

            Integer id = keyword.getId();
            String keyword1 = keyword.getKeyword();
            String url = keyword.getUrl();
            Boolean isHot = keyword.getIsHot();
            Boolean isDefault = keyword.getIsDefault();
            Integer sortOrder = keyword.getSortOrder();
            String addTime = keyword.getAddTime();
            String updateTime = keyword.getUpdateTime();
            Boolean deleted = keyword.getDeleted();

            WxSearchIndexVO.HotKeywordListBean hotKeywordListBean = new WxSearchIndexVO.HotKeywordListBean();
            hotKeywordListBean.setAddTime(addTime);
            hotKeywordListBean.setDefault(isDefault);
            hotKeywordListBean.setDeleted(deleted);
            hotKeywordListBean.setHot(isHot);
            hotKeywordListBean.setId(id);
            hotKeywordListBean.setUrl(url);
            hotKeywordListBean.setSortOrder(sortOrder);
            hotKeywordListBean.setKeyword(keyword1);
            hotKeywordListBean.setUpdateTime(updateTime);

            hotKeywordListBeans.add(hotKeywordListBean);

            //2.2
            if (isHot.equals(true)) {
                defaultKeywordBean.setAddTime(addTime);
                defaultKeywordBean.setDefault(isDefault);
                defaultKeywordBean.setDeleted(deleted);
                defaultKeywordBean.setHot(isHot);
                defaultKeywordBean.setId(id);
                defaultKeywordBean.setUrl(url);
                defaultKeywordBean.setSortOrder(sortOrder);
                defaultKeywordBean.setKeyword(keyword1);
                defaultKeywordBean.setUpdateTime(updateTime);
            }
        }

        //3.
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        //TODO
        //需要从session中拿到一个userID，用这个useriD进行历史记录的查询,这样查询出来的
        // 是所有的历史记录，
        for (SearchHistory searchHistory : searchHistories) {
           if (searchHistory.getUserId().equals(1)){
               String keyword = searchHistory.getKeyword();
               WxSearchIndexVO.HistoryKeywordListBean historyKeywordListBean = new WxSearchIndexVO.HistoryKeywordListBean();
               if (!"".equals(keyword)){
                   historyKeywordListBean.setKeyword(keyword);
                   historyKeywordListBeans.add(historyKeywordListBean);
               }
           }
        }
        //5.
        wxSearchIndexVO.setDefaultKeyword(defaultKeywordBean);
        wxSearchIndexVO.setHistoryKeywordList(historyKeywordListBeans);
        wxSearchIndexVO.setHotKeywordList(hotKeywordListBeans);
        return wxSearchIndexVO;
    }

    @Override
    public void addHistory(String keyword) {
        SearchHistory searchHistory = new SearchHistory();
        Date date = new Date(System.currentTimeMillis());
        //TODO
        //这里需要把user_id拼接进去,从session域中拿出来
        Integer userId = UserIdUtil.getUserId();
        searchHistory.setUserId(userId);
        if (keyword == null){
            keyword= "";
        }
        searchHistory.setKeyword(keyword);
        searchHistory.setDeleted(false);
        searchHistory.setAddTime(date);
        //TODO
        //不知道这里写什么form
        searchHistory.setFrom("wx");
        searchHistory.setUpdateTime(date);
        //怎么实现两个时间不一样
        //TODO
        searchHistoryMapper.insert(searchHistory);
    }


}
